package com.cke.yang.storage.api.controller;

import com.cke.yang.common.domain.model.Result;
import com.cke.yang.common.infra.utils.UserDetailsUtils;
import com.cke.yang.storage.api.dto.request.StgSourceListRequest;
import com.cke.yang.storage.app.service.StgSourceService;
import com.cke.yang.storage.domain.entity.StgSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: luozhaojian
 * @description:
 * @date: 2025/10/7 10:52
 */
@RestController
@RequestMapping("/api/storage/stg-source")
public class StgSourceController {

    @Autowired
    private StgSourceService stgSourceService;

    @PostMapping("/save")
    public Result<StgSource> save(@RequestBody StgSource stgSource) {
        return Result.success(stgSourceService.save(stgSource));
    }

    @GetMapping("/self")
    public Result<List<StgSource>> self() {
        Long currentUserId = UserDetailsUtils.getCurrentUserId();
        if (currentUserId == null) {
            throw new RuntimeException("用户未登录");
        }
        List<StgSource> stgSources = stgSourceService.findByUserId(currentUserId);
        return Result.success(stgSources);
    }

    @PostMapping("/list")
    public Result<List<StgSource>> list(@RequestBody StgSourceListRequest request) {
        List<StgSource> stgSources = stgSourceService.list(request);
        return Result.success(stgSources);
    }

}
